package com.example.fszq.volleydemo1.thread;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.example.fszq.volleydemo1.DisplayMessageActivity;
import com.example.fszq.volleydemo1.utils.MyUtil;
import com.qiniu.android.common.Zone;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fszq on 2015/12/31.
 */
public class UploadImageThread extends AsyncTask<Void, Integer, Void> {
    private boolean isCancled;
    private int taskCount = 0,taskIdx = 0;
    private ProgressDialog mProgressDialog;
    private String uToken = "",filePath = "",fileName = "";
    private UploadManager uploadManager;
    private Context context;
    private List<String> uPath = new ArrayList<>();

    private MyUtil myUtil ;

    public UploadImageThread(Context context,ProgressDialog mPD,String[] uInfo,List<String> uPath) {
        this.taskCount = Integer.parseInt(uInfo[2]);
        this.taskIdx = Integer.parseInt(uInfo[1]);
        //this.mProgressDialog = mPD;
        this.uToken = uInfo[0];
        this.filePath = uInfo[3];
        this.context = context;
        this.myUtil =  new MyUtil(context);
        this.fileName = myUtil.getShared("userName") + "_" + String.valueOf(System.currentTimeMillis());
        this.uPath = uPath;

        //初始化七牛云
        Configuration qnconfig = new Configuration.Builder()
                .chunkSize(256 * 1024)  //分片上传时，每片的大小。 默认 256K
                .putThreshhold(512 * 1024)  // 启用分片上传阀值。默认 512K
                .connectTimeout(10) // 链接超时。默认 10秒
                .responseTimeout(60) // 服务器响应超时。默认 60秒
                        //.recorder(recorder)  // recorder 分片上传时，已上传片记录器。默认 null
                        //.recorder(recorder, keyGen)  // keyGen 分片上传时，生成标识符，用于片记录器区分是那个文件的上传记录
                .zone(Zone.zone0) // 设置区域，指定不同区域的上传域名、备用域名、备用IP。默认 Zone.zone0
                .build();

        // 重用 uploadManager。一般地，只需要创建一个 uploadManager 对象
        uploadManager = new UploadManager(qnconfig);

    }

    @Override
    protected void onPreExecute() {
        mProgressDialog.show(context,null,"附件图片上传中......");
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected Void doInBackground(Void... params) {
        if (!isCancelled() && isCancled == false) // 这个地方很关键，如果不设置标志位的话，直接setCancel（true）是无效的
        {
            int prog = 0;

            uploadManager.put(filePath, fileName, this.uToken,
                    new UpCompletionHandler() {
                        @Override
                        public void complete(String key, ResponseInfo info, JSONObject res) {
                            //  res 包含hash、key等信息，具体字段取决于上传策略的设置
                            Log.i("qiniu", key + ",\r\n " + info + ",\r\n " + res);
                            uPath.add("http://7xpdyg.com1.z0.glb.clouddn.com/" + key);
                        }


                    },
                    new UploadOptions(null, null, false,
                            new UpProgressHandler(){
                                public void progress(String key, double percent){
                                    publishProgress((int)percent*100); // 更新进度条
                                    Log.i("qiniu", key + ": " + percent);
                                }
                            }, null)
                    );


        }
        //                publishProgress(prog); // 更新进度条

        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        //if(taskCount == (taskIdx + 1)) {
        mProgressDialog.dismiss();
        //}
        //记入图片记录数组
        Log.d("uPath", uPath.size()+"");

        Log.d("result", taskIdx + "ok");
    }

    @Override
    protected void onProgressUpdate(Integer... values)
    {
        //mTaskItem.setProgress(values[0]); // 设置进度
        mProgressDialog.setMessage("处理任务" + taskIdx + ":" + values[0] + "%");
    }
}
